<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="2" :sm="12">
            <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select v-model="queryParam.condition">
                <a-select-option key="PrincipalName">业务员</a-select-option>
                <a-select-option key="cCusName">客户名称</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input allowClear @keydown="inputKeyDown" v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-range-picker :value="time" @change="onAllocateTimeChange" />
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-button type="primary" @click="() => { this.pagination.current = 1; this.getDataList() }">查询</a-button>
            <a-button style="margin-left: 8px" @click="repossess()">重置</a-button>
            <a-button style="margin-left: 8px" @click="ExportDataToExcel()" type="primary" v-if="isShowExport"><a-icon
                type="download" />导出数据</a-button>
          </a-col>
          <a-col :md="5" :sm="24">
            <a-radio-group v-model="sorter.field" style="float: right" default-value="PrincipalName"
              @change="getDataList()" button-style="solid">
              <a-radio-button value="PrincipalName"> <a-icon type="arrow-down" />&nbsp;业务员&nbsp;</a-radio-button>
              <a-radio-button value="cCusName"> <a-icon type="arrow-down" />&nbsp;客户名称&nbsp;</a-radio-button>
              <a-radio-button value="期末余额"> <a-icon type="arrow-down" />&nbsp;期末余额&nbsp;</a-radio-button>
            </a-radio-group>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table ref="table" :scroll="{ y: tableHeight }" :columns="columns" :rowKey="(row, index) => index" :dataSource="data"
      :pagination="pagination" :loading="loading" @change="handleTableChange" :bordered="true" size="small">
      <span slot="期初余额" slot-scope="期初余额" v-format="'#,##0'">{{ Number(期初余额) }}</span>
      <span slot="本期发货" slot-scope="本期发货" v-format="'#,##0'">{{ Number(本期发货) }}</span>
      <span slot="本期收款" slot-scope="本期收款" v-format="'#,##0'">{{ Number(本期收款) }}</span>
      <span slot="期末余额" slot-scope="期末余额" v-format="'#,##0'">{{ Number(期末余额) }}</span>
      <span slot="呆账" slot-scope="呆账" v-format="'#,##0'">{{ Number(呆账) }}</span>
    </a-table>
  </a-card>
</template>

<script>
import moment from 'moment'
import { download } from '@/components/download'
const columns = [
  { title: '业务员', dataIndex: 'PrincipalName', align: 'center', width: '6%' },
  { title: '客户编码', dataIndex: 'cCusCode', align: 'left', width: '10%' },
  { title: '客户名称', dataIndex: 'cCusName', align: 'left' },
  { title: '期初余额', dataIndex: '期初余额', align: 'right', width: '12%', scopedSlots: { customRender: '期初余额' } },
  { title: '本期发货', dataIndex: '本期发货', align: 'right', width: '12%', scopedSlots: { customRender: '本期发货' } },
  { title: '本期收款', dataIndex: '本期收款', align: 'right', width: '12%', scopedSlots: { customRender: '本期收款' } },
  { title: '期末余额', dataIndex: '期末余额', align: 'right', width: '12%', scopedSlots: { customRender: '期末余额' } },
  { title: '呆账', dataIndex: '呆账', align: 'right', width: '12%', scopedSlots: { customRender: '呆账' } }
]

export default {
  name: 'ReceivableAccountsDetail',
  components: {},
  mounted() {
    this.time = [moment(this.startTime, 'YYYY-MM-DD'), moment(this.endTime, 'YYYY-MM-DD')]
    this.queryParam.sDate = this.startTime
    this.queryParam.dDate = this.endTime
    this.getDataList()
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true)
    })
  },
  data() {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 50,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'PrincipalName', order: 'DESC' },
      loading: false,
      columns,
      queryParam: {
        sDate: moment(new Date()).month(new Date().getMonth()).date(1).startOf('month').format('YYYY-MM-DD'),
        dDate: moment(new Date()).month(new Date().getMonth()).date(1).endOf('month').format('YYYY-MM-DD'),
        condition: 'PrincipalName',
        keyword: ''
      },
      time: null,
      isShowExport: false, // 是否显示导出按钮
      startTime: moment(new Date()).month(new Date().getMonth()).date(1).startOf('month').format('YYYY-MM-DD'),
      endTime: moment(new Date()).month(new Date().getMonth()).date(1).endOf('month').format('YYYY-MM-DD')
    }
  },
  methods: {
    onAllocateTimeChange(dates, dateStrings) {
      this.queryParam.sDate = dateStrings[0]
      this.queryParam.dDate = dateStrings[1]
      this.time = dates
    },
    repossess() {
      this.queryParam = {
        sDate: this.startTime,
        dDate: this.endTime,
        condition: 'PrincipalName',
        keyword: ''
      }
      this.sorter.field = 'PrincipalName'
      this.time = [moment(this.startTime, 'YYYY-MM-DD'), moment(this.endTime, 'YYYY-MM-DD')]
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_Manage/Home/GetReceivableAccountsDetail', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order, DeptId: this.DeptId,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
          if (this.data && this.data.length > 0) {
            this.isShowExport = true
          }
        })
    },
    ExportDataToExcel() {
      const that = this
      // this.$confirm({
      //   title: '提示',
      //   content: '是否导出按当前条件查询的全部记录到Excel ?',
      //   onOk () {
      //     that.$http
      //       .post('/Base_Manage/Home/ExportDetailToExcel', {
      //         SortField: that.sorter.field || 'Id',
      //         SortType: that.sorter.order,
      //         Search: that.queryParam,
      //         ...that.filters
      //       })
      //       .then(resJson => {
      //         if (resJson.Success) {
      //           download(resJson.Data.fileUrl, resJson.Data.fileName)
      //         } else {
      //           this.$message.error(resJson.Msg)
      //         }
      //       })
      //   }
      // })

      this.$confirm('是否导出按当前条件查询的全部记录到Excel ?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.$http
          .post('/Base_Manage/Home/ExportDetailToExcel', {
            SortField: that.sorter.field || 'Id',
            SortType: that.sorter.order,
            Search: that.queryParam,
            ...that.filters
          })
          .then(resJson => {
            // resolve()

            if (resJson.Success) {
              download(resJson.Data.fileUrl, resJson.Data.fileName)
            } else {
              that.$message.error(resJson.Msg)
            }
          })
      })
    },
    inputKeyDown(e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    timeFormat(val) { // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
